﻿using System;
using System.Diagnostics;

namespace _100_使用SpanT提升处理字符串的性能_集合
{
    class Program
    {
        static void Main(string[] args)
        {
            string 我的 = "我家养了20只猫";

            //常规方法
            Stopwatch swl = Stopwatch.StartNew();
            for (int x = 0; x < 1000000; x++)
            {
                int v = int.Parse(我的.Substring(5, 2));
            }
            swl.Stop();
            Console.WriteLine($"常规方法：耗时{swl.ElapsedMilliseconds}ms");

            //使用span<T>方法
            Stopwatch sw2 = Stopwatch.StartNew();
            ReadOnlySpan<char> span = 我的.ToCharArray();
            for (int i = 0; i < 1000000; i++)
            {
                int v = 0;
                var subspan = span.Slice(5, 2);
                for (int cc = 0; cc < subspan.Length; cc++)
                {
                    char ch = subspan[cc];
                    v = (ch - '0') + v * 10;
                }
            }
            sw2.Stop();
            Console.WriteLine($"使用span:耗时{sw2.ElapsedMilliseconds} ms");
        }
    }
}
